package Luogu;

import java.util.Scanner;

public class P1048 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int V = sc.nextInt();
        int n = sc.nextInt();
        int[] vs = new int[n + 1];
        int[] ws = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            ws[i] = sc.nextInt();
            vs[i] = sc.nextInt();
        }
        int[][] dp = new int[n+1][V + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= V; j++) {
                if (j >= ws[i]) {
                    dp[i][j] = Math.max(dp[i - 1][j - ws[i]] + vs[i], dp[i - 1][j]);
                } else {
                    dp[i][j] = dp[i - 1][j];
                }

            }
        }
        System.out.println(dp[n][V]);
    }
}
